﻿using System;

namespace CardLib
{
	// collection 版本deck
	public class DeckCol:ICloneable
	{
		private Cards cards;

		public DeckCol(Cards newCards)
		{
			cards = newCards;
		}

		public DeckCol()
		{
			for(int suitVal = 0; suitVal < 4; suitVal ++ )
			{
				for (int rankVal = 1; rankVal < 14; rankVal++)
				{
					cards.Add(new Card((Suit)suitVal, (Rank)rankVal));
				}
			}
		}

		public Card GetCard(int cardNum)
		{
			if ((cardNum >= 0)&&(cardNum <= 51))
			{
				return cards[cardNum];
			}
			throw  new CardOutOfRangeException(cards);
			// throw new System.ArgumentOutOfRangeException("cardNum", cardNum, "index out");
		}

		public void Shuffle()
		{
			Cards newCards = new Cards();
			bool []records = new bool[52];
		
			Random ranGen = new Random();
			for (int i = 0; i < 52; i++)
			{
				while (true)
				{
					int ranNum = ranGen.Next(52);

					if (records[ranNum] == false)
					{
						newCards.Add(cards[ranNum]);
						records[ranNum] = true;
						break;
					}
				}
			}
			newCards.CopyTo(cards);
		}

		public object Clone()
		{
			Cards newCards = cards.Clone() as Cards;
			
			return new DeckCol(newCards);
		}
	}
}